<!DOCTYPE html>
<html lang="en">
	<head>
		<meta charset="UTF-8" />
		<title>WebSocket 多终端通信</title>
	</head>
	<body>
		<input type="text" id="msg"/>
		<button onclick="send()">发送</button>
		<ul id="content">
		</ul>
		
		<script type="text/javascript">
			// 创建websocket对象
			var ws = new WebSocket("ws://localhost:1234/");
			
			var inputNode = document.getElementById("msg");
			var content = document.getElementById("content");
			
			// 建立连接时触发
			ws.onopen = function(res){
				console.log("连接成功。。。");
			}
			
			// 接收到服务器推送数据触发
			ws.onmessage = function(msg){
				content.innerHTML+= "<li>"+msg.data+"</li>";
			}
			
			// 发送数据
			function send(){
				ws.send(inputNode.value);
				inputNode.value = "";
			}
		</script>
	</body>
</html>